local shibing = fk.CreateSkill{
  name = "sclan__shibing_active&",
}

Fk:loadTranslationTable{
  ["sclan__shibing_active&"] = "石兵",
  [":sclan__shibing_active&"] = "出牌阶段限一次，你可以用一张手牌替换一张「石兵」，替换后的「石兵」置于末位。",
  ["#sclan__shibing_active&"] = "石兵：可以用一张手牌替换一张「石兵」",

  ["shibingPoxi"] = "石兵",
  ["#shibingPoxi-prompt"] = "石兵：用一张手牌替换一张「石兵」",
}

Fk:addPoxiMethod{
  name = "shibingPoxi",
  prompt = "#shibingPoxi-prompt",
  cancelable = false,
  card_filter = Util.TrueFunc,
  feasible = function(selected)
    return #selected == 2 and not table.every(selected, function (c)
      return Fk:currentRoom():getCardArea(c) == Card.PlayerHand
    end)
  end,
  default_choice = function (data)
    return {data["sclan__shibing"][1], data["$Hand"][1]}
  end,
}

shibing:addEffect("active", {
  anim_type = "special",
  prompt = "#sclan__shibing_active&",
  can_use = function (self, player)
    return player:usedSkillTimes(shibing.name, Player.HistoryPhase) == 0
    and not player:isKongcheng()
  end,
  card_num = 0,
  target_num = 1,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected)
    return #selected == 0 and to_select:hasSkill("sclan__shibing")
    and #to_select:getPile("sclan__shibing") > 0
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    local target = effect.tos[1]

    local cards = room:askToPoxi(player, {
      poxi_type = "shibingPoxi",
      data = {
        {"sclan__shibing", target:getPile("sclan__shibing")},
        {"$Hand", player:getCardIds("h")},
      },
      extra_data = {
        min = 2,
        max = 2,
        skillName = "sclan__shibing",
      }
    })

    if #cards == 2 then
      table.sort(cards, function (a, b)
        return room:getCardArea(a) < room:getCardArea(b)
      end)
      room:moveCards({
        ids = {cards[1]},
        from = player,
        to = target,
        toArea = Card.PlayerSpecial,
        specialName = "sclan__shibing",
        proposer = player,
        skillName = "sclan__shibing",
        moveReason = fk.ReasonExchange,
        moveVisible = true,
      }, {
        ids = {cards[2]},
        from = target,
        to = player,
        toArea = Card.PlayerHand,
        specialName = "sclan__shibing",
        proposer = player,
        skillName = "sclan__shibing",
        moveReason = fk.ReasonExchange,
        moveVisible = true,
      })
    end
  end
})

return shibing